using System;
using SmartBoard.Book.Model;

namespace SmartBoard.Book.Commands
{
    public class CreateNewBookAction : BookUIActionBase
    {
        public CreateNewBookAction(BookManager bookManager, IBookUIManager bookUIManager):base(bookManager, bookUIManager)
        { }
        #region ICommand Members

        public override bool CanExecute(object parameter)
        {
            return true;
        }

        public override void Execute(object parameter)
        {
            if(CanCreateBook())
            {
                var dialog = _bookUIManager.CreateNewBookUIDialog;
                if (dialog.PresentUI() == true)
                {
                    var b = new Model.Book
                    {
                        Name = dialog.BookName
                    };
                    while (b.Pages.Count < dialog.PageCount)
                    {
                        b.Pages.Add(new Page());
                    }
                    _bookManager.Book = b;
                }
            }
        }
        #endregion

        private bool CanCreateBook()
        {
            if (_bookManager.Book == null)
                return true;
            if (_bookManager.Book.IsEmpty)
                return true;
            return _bookUIManager.CanCreateNewBookUIDialog.PresentUI() == true;
        }
    }
}